<?php

/**
 * seller actions.
 *
 * @package    ndirou_affaire
 * @subpackage seller
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sellerActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeLogout(sfWebRequest $request)
  {
    $this->getUser()->setAuthenticated(false);
    $this->getUser()->setAttribute('seller',null);
    $this->redirect('@homepage');
  }
  
  /**
   * Login
   * @param sfWebRequest $request 
   */
  public function executeLogin(sfWebRequest $request)
  {
    $this->setLayout('popin');
    $this->loginform = new LoginForm();
    if( $request->isMethod('post') && $request->hasParameter('login') ){
        $data = $request->getParameter('login');
        $this->loginform->bind($data);
        $this->setLayout('layout');
        if( $this->loginform->isValid() ){
        	$this->getUser()->setAuthenticated(true);
        	$seller = Doctrine_Core::getTable("Seller")->findOneByEmailAndPassword($this->loginform->getValue('email'), $this->loginform->getValue('password'));
        	$this->getUser()->setAttribute('seller', $seller->getFullname() );
        	$this->getUser()->setAttribute('sellerId',$seller->id);
                $this->redirect( $this->getUser()->getAttribute('referer') );
        }
    }
  }
  
  /**
   * Creation d'un compte
   * 
   */
  public function executeNew(sfWebRequest $request){
  	if( !$this->getUser()->isAuthenticated() ){
  		$this->form = new SellerForm();
  		$this->form->useFields( array('gender','firstname','lastname','email','mobile','password','passwordConfirm','zipcode','town_id','newsletter') );
  		$this->form->setDefault('gender','mr');
  		if($request->isMethod('post') && $request->hasParameter('subscription')){
  			$data = $request->getParameter('subscription');
	        $this->form->bind($data);
	        if ($this->form->isValid()){
	        	$seller = $this->form->getObject(); 
	        	$seller->SynchronizeWithArray($data);
	        	$seller->setCountry('DZ');
	        	$seller->save();
	        	$this->getUser()->setAuthenticated(true);
    			$this->getUser()->setAttribute('seller',$seller->getFullname());
    			$this->getUser()->setAttribute('sellerId',$seller->id);
    			$this->getUser()->setFlash('success',"Félicitation");
	        }
  		}
  	}else{
  		$this->redirect('@homepage');
  	}
  }
  
  /**
   * mot de passe oublié
   */
    public function executeLostPassword(sfWebRequest $request) {
      $this->form = new PasswordForm;
      if($request->isMethod('post') && $request->hasParameter('password')){
          $data = $request->getParameter('password');
          $this->form->bind($data);
           if ($this->form->isValid()){
               // echo '<pre>' . print_r($this->form->getValues() ,1) . '</pre>';
                //echo $password = $this->form->getValue('password_field');
                //echo $this->form->getValue('firstname_field');
                //envoi de mail au client
                $this->dispatcher->notify(new sfEvent($this, 'seller.lostPassword',array( 
                    'password' => $this->form->getValue('password_field'), 
                    'firstname' => $this->form->getValue('firstname_field'),
                    'email' => $this->form->getValue('email')  )));
                
                 $this->getUser()->setFlash('success', 'Vous allez reçevoir votre mot de passe sur votre adresse E-Mail');
          }
      }
  }
  
  /**
   * mes annonces
   */
  public function executeMyAds(sfWebRequest $request){
      if( $this->getUser()->isAuthenticated() ){
          $sellerId = $this->getUser()->getAttribute('sellerId');
          $this->aAds = Doctrine_Core::getTable('Ads')->getSellerAdsList($sellerId);
      }  else {
          $this->getUser()->setAttribute('referer', '@myAds');
          $this->redirect('@login');
      }
  }
  
  /**
   * Editer un compte
   */
  public function executeEdit(sfWebRequest $request){
      if( $this->getUser()->isAuthenticated() ){
            $seller = Doctrine_Core::getTable('Seller')->find( $this->getUser()->getAttribute('sellerId') );
            $this->form = new SellerForm($seller);
            $this->form->useFields( array('gender','firstname','lastname','email','mobile','password','passwordConfirm','zipcode','town_id','newsletter') );
            $this->form->setDefault('gender','mr');
            if($request->isMethod('post') && $request->hasParameter('subscription')){
                $data = $request->getParameter('subscription');
                $this->form->bind($data);
                if ($this->form->isValid()){
                    $seller = $this->form->getObject(); 
                    $seller->SynchronizeWithArray($data);
                    //$seller->setCountry('DZ');
                    $seller->save();
                    $this->getUser()->setFlash('success',"Votre compte a bien été mis à jour");
                    $this->redirect('@homepage');
                }
            }
  	}else{
            $this->redirect('@homepage');
  	}
  }
}
